<?php
/**
 * Conversations - A lightweight BB for Xaraya
 *
 * @package modules
 * @copyright (C) 2003-2006 The Digital Development Foundation.
 * @license GPL {@link http://www.gnu.org/licenses/gpl.html}
 * @link http://www.xaraya.com
 *
 * @subpackage conversations
 * @link http://xaraya.com/index.php/release/760.html
 * @author John Cox
*/
/**
 * Look up tags to posts
 * @returns array
 * @return array of zero or more subscriptions, or NULL on failure
 * @param uid integer
 * @param tagid integer
 */

function conversations_userapi_getlook($args)
{
    extract($args);

    // Optional arguments
    if (!isset($startnum)) {$startnum = 1;}
    if (!isset($numitems)) {$numitems = -1;}

    $dbconn =& xarDBGetConn();
    $xartable =& xarDBGetTables();
    $clook = $xartable['clook'];

    $query = "SELECT DISTINCT xar_tagid, xar_postid "
            . " FROM $clook";

    $where = array();
    $bind = array();

    if (!empty($postid)) {
        $where[] = 'xar_postid = ?';
        $bind[] = (int)$postid;
    }

    if (!empty($tagid)) {
        $where[] = 'xar_tagid = ?';
        $bind[] = (int)$tagid;
    }

    if (!empty($where)) {
        $query .= ' WHERE ' . implode(' AND ', $where);
    }

    // Not really necessary
    $query .= " ORDER BY xar_tagid";

    $result =& $dbconn->SelectLimit($query, $numitems, $startnum-1, $bind);
    if (!$result) return;

    $subs = array();
    for (; !$result->EOF; $result->MoveNext()) {
        list($tagid, $postid) = $result->fields;

        $subs[] = array(
                'tagid'     => $tagid,
                'postid'    => $postid
        );
    }
    $result->Close();
    return $subs;
}
?>